﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace App_Code.Helpers
{
    /// <summary>
    /// Summary description for TopdownMeExtractor
    /// </summary>
    public class TopdownMeExtractor
    {
        public static List<string> GetMovieUrls(string content)
        {
            var movieUrls = new List<string>();       

            const string linkRegEx = "'images/MirrorBg\\.png'(.+?)</td";
            var matches = Regex.Matches(content, linkRegEx, RegexOptions.Singleline);
            foreach(Match match in matches)
            {
                var linkSection = match.Groups[1].Value;
                if (IsContainVideoLink(linkSection))
                {
                    const string videoRegEx = "<a href=\"(.+?)\"";
                    var links = Regex.Matches(linkSection, videoRegEx, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                    foreach (var vlink in
                        links.Cast<Match>().Where(vlink => !movieUrls.Contains(vlink.Groups[1].Value)))
                    {
                        if (!IsMultiUploadVideoLink(vlink.Groups[1].Value))
                            movieUrls.Add(vlink.Groups[1].Value);
                        else
                        {
                            var movieLink = ExtractMovieUrl(vlink.Groups[1].Value, "Megaupload");
                            movieUrls.Add(movieLink);
                        }
                    }
                }
            }

            return movieUrls;
        }

        public static string ExtractMovieUrl(string movieUrl, string FileHostingName)
        {
            var pageContent = Utils.GetPage(movieUrl, Encoding.UTF8);
            var multiUploadVideoLink = Regex.Match(   pageContent,
                                            string.Format("alt=\"{0}\".*?<a href=\"(.+?)\"", FileHostingName), 
                                            RegexOptions.Singleline).Groups[1].Value;

            var megaUploadPageContent = Utils.GetPage(multiUploadVideoLink, Encoding.UTF8);
            var megaVideoVideoLink = Regex.Match(   megaUploadPageContent,
                                                    "http://www.megavideo.com/.d=(.+?)\"",
                                                    RegexOptions.Singleline).Value;
            if(!string.IsNullOrEmpty(megaVideoVideoLink))
            {
                return megaVideoVideoLink.Replace("\"", string.Empty);    
            }

            var megaUploadVideoLink = Regex.Match(  megaUploadPageContent,
                                                    "http://www.megaupload.com/.d=(.+?)\"",
                                                    RegexOptions.Singleline).Value;
            return megaUploadVideoLink.Replace("\"", string.Empty);
        }

        private static bool IsMultiUploadVideoLink(string videoLink)
        {
            return videoLink.ToLower().Contains("multiupload");
        }

        public static bool IsContainVideoLink(string content)
        {
            return  content.ToLower().Contains("multiupload")   ||
                    content.ToLower().Contains("mytube")        ||
                    content.ToLower().Contains("megaupload")    ||
                    content.ToLower().Contains("megavideo");
        }
    }
}